define(function(require, exports, module) {
	"use strict";
	document.title = "";
	require("../../dep/mui/js/mui.min.js");
	require("../jquery-3.1.1.min.js");
	require("../mui.picker.min.js");
	require("../dateShow.js");
	require("./dateInput.js");
	var globalInit = require("globalInit"); //共用js
	var muiTips = require("muiTips");
	require("keepDate");
	var template = require("template");
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');
	dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏
	var examinaSourceId = 0; //启用审批需传参数

	var abpUse = require("../abp.js");
	var abp = abpUse.abpstart;

	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	var getFunList = require("../getFunList/getFunList.js"); //函数配置js
	if(sessionStorage.changeOrderflag) {
		getFunList.changeOrderDetail("goodsList", "#goodsListShow"); //编辑订单详情	//计算价格
		var orderData = JSON.parse(sessionStorage.getItem("orderInDetail"));
		examinaSourceId = Number(orderData.id); //修改启用审批需传参数
		//			sessionStorage.removeItem("flag");

	} else {
		getFunList.addGoodsInfoList("goodsList", "#goodsListShow"); //新增订单选择货品列表展示
		getFunList.newOutstockCode(); //新增出库单号自动生成        客户 业务员自动生成
		getFunList.btnShowAndHide(); //按钮隐藏判断函数
	}
	var outSourceType = sessionStorage.getItem("outSourceType");
	if(outSourceType == abp.sourceType.WH_CHECK) {
		$("#mui-choice").hide();
	}
	$(function() {

		//设置头部信息
		if(sessionStorage.changeOrderflag) {
			$(".mui-title").html("编辑库存入库");
		} else {
			$(".mui-title").html("新增库存入库");
		}
		dingTalkNav.setTitleText($(".mui-title").html());

		function clearSession() {
			sessionStorage.removeItem("goodsInfoList"); //货品列表信息
			sessionStorage.removeItem("chioceStaff"); //业务员信息
			sessionStorage.removeItem("chioceStaffId");
			sessionStorage.removeItem("chioceSupplierName"); //供应商信息	
			sessionStorage.removeItem("chioceSupplierNameId");
			sessionStorage.removeItem("chioceClientName"); //客户信息	
			sessionStorage.removeItem("chioceClientNameId");
			sessionStorage.removeItem("flag");
			sessionStorage.removeItem("money");
			sessionStorage.removeItem('createHome');
		}
		//清除缓存钉钉
		function clearSessionDDUpdate() { //新增订单页面提交 清除本地存储
			clearSession();
			document.location.href = "../../src/outStock/outStockDetail.html?dd_nav_bgcolor=FFFF943E";
		}
		//清除缓存钉钉
		function clearSessionDDCreate() { //新增订单页面提交 清除本地存储
			clearSession();
			document.location.href = "../../src/outStock/index.html?dd_nav_bgcolor=FFFF943E";
		}
		//清除缓存钉钉
		function clearSessionDDHome() { //新增订单页面提交 清除本地存储
			clearSession();
			document.location.href = "../../src/index.html?dd_nav_bgcolor=FFFF943E";
		}
		if(sessionStorage.changeOrderflag) {
			$(".mui-title").text("编辑库存出库");
			//钉钉返回清除缓存
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDUpdate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDUpdate);
		} else if(sessionStorage.createHome) {
			$(".mui-title").text("新增库存出库");
			//钉钉返回清除缓存
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDHome);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDHome);
		} else {
			$(".mui-title").text("新增库存出库");
			//钉钉返回清除缓存
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDCreate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDCreate);
		}

		getFunList.settlement(); //计算货品总价
		//删除货品明细列表
		var updateSell = require("./updateSell.js");
		//计算数量与金额 删除明细
		if(sessionStorage.goodsInfoList) {
			//计算总数量与金额
			//			updateSell.allnumAndAllMoney();

			//删除订单明细
			var goodsLists = JSON.parse(sessionStorage.goodsInfoList);
			updateSell.deleteGoodsDetail({
				"delete": ".delete-trash", //删除按钮
				"array": goodsLists, //需要删除的数组
				"goodsIn": ".production-list", //移除页面元素
				"goodsInfoList": "goodsInfoList", //需要存储的数组对象
			}, getFunList.settlement);
		}

		function newOutstock(statusId) { //请求函数
			var serialNo = $("#code").text().trim();
			var dataCreated = $(".createDate").text().trim(); //创建日期
			var deliveryDate = $(".lastDate").text().trim(); //预交日期
			//			var companyId = Number($("#supplier").attr("data-id")); //客户ID
			var busUserId = Number($("#buyer").attr("data-id")); //业务员ID
			var notes = $(".remarks").val().trim();

			var companyId = 0;
			if(sessionStorage.chioceSupplierNameId) {
				companyId = sessionStorage.chioceClientNameId;
			}

			var option = {};

			var dataModel = {
				"sourceType": 0, //(integer, optional): 来源单类型 ,
				"sourceSerial": "", //(string, optional): 来源单号 ,
				"sourceId": 0, //(integer): 来源单id ,
				//					"whOutputDetail": [],	//(Array[WhInputDetailInput], optional): 库存出库明细 ,
				"serialNo": serialNo, //(string): 单号 ,
				"serialDate": dataCreated, //(string): 单据日期 ,
				"companyId": companyId, //(integer): 客户 ,
				"contactId": 0, //(integer): 联系人 ,
				"busUserId": busUserId, //(integer): 业务员 ,
				"deptId": 0, //(integer): 部门 ,
				"statusId": statusId, //(integer): 提交状态 ,
				"notes": notes, //(string): 备注 ,
				"id": 0 //(integer, optional): Id
			};
			dataModel.whOutputDetail = [];

			var productionList = $(".production-list");
			for(var i = 0; i < productionList.length; i++) {

				var whId = Number($(".production-list").eq(i).find(".warehouse").data("id"));
				var goodsId = Number($(".production-list").eq(i).find(".clothes-code").data("id"));
				var price = Number($(".production-list").eq(i).find(".price").text());
				var goodsUnit = $(".production-list").eq(i).find(".clothes-code").data("unit");
				var goodsNum = Number($(".production-list").eq(i).find(".goodsNum").text());
				var whOutputDetail = {
					"whId": whId, //(integer): 仓库 ,
					"goodsId": goodsId, //(integer): 货品 ,
					"storageId": 0, //(integer): 储位 ,
					"unit": goodsUnit, //(string): 主单位 ,
					"price": price, //(number): 单价 ,
					"unitCode": goodsUnit, //(string): 副单位 ,
					"unitCodeNumber": goodsNum, //(number): 副单位数量 ,
					"unitCodeConvertRate": 1, //(number): 副单位转换率 ,
					"notes": "111", //(string, optional): 备注 ,
					"sourceType": 0, //(integer, optional): 来源单类型 ,
					"sourceSerial": "", //(string, optional): 来源单编号 ,
					"sourceId": 0, //(integer, optional): 来源单id ,
					"sourceItemId": 0, //(integer, optional): 来源单明细id ,
					"batchDetail": [],
					"id": 0
				};
				dataModel.whOutputDetail.push(whOutputDetail);
			}
			//	        console.log(dataModel.whOutputDetail);

			abp.services.app.whOutputList.create(dataModel, option)
				.done(function(data) {
					//console.log(data);

					function locaHref() {
						document.location.href = "../outStock/index.html";
					}
					if(dataModel.statusId == abp.statusId.Draft) {

						muiTips.alertTips(' ', "保存成功", 1, 3, locaHref);
					} else {

						muiTips.alertTips(' ', "提交成功", 1, 3, locaHref);
					}
					sessionStorage.removeItem('createHome');
					getFunList.clearSessionStroage();

				})
				.fail(function(data) {

					muiTips.alertTips(' ', data.message, 2, 3, "default");
					globalInit.buttonReset(".btnDisabledText");
				});
		}

		function changeOutstock(statusId) { //请求函数
			var serialNo = $("#code").text().trim();
			var id = $("#code").attr("data-id");
			var dataCreated = $(".createDate").text().trim(); //创建日期
			var deliveryDate = $(".lastDate").text().trim(); //预交日期
			var companyId = Number($("#companyName").attr("data-id")); //客户ID
			var busUserId = Number($("#buyer").attr("data-id")); //业务员ID
			var notes = $(".remarks").val().trim();

			var option = {};

			var dataModel = {
				"sourceType": 0, //(integer, optional): 来源单类型 ,
				"sourceSerial": "", //(string, optional): 来源单号 ,
				"sourceId": 0, //(integer): 来源单id ,
				//					"whOutputDetail": [],	//(Array[WhInputDetailInput], optional): 库存出库明细 ,
				"serialNo": serialNo, //(string): 单号 ,
				"serialDate": dataCreated, //(string): 单据日期 ,
				"companyId": companyId, //(integer): 客户 ,
				"contactId": 0, //(integer): 联系人 ,
				"busUserId": busUserId, //(integer): 业务员 ,
				"deptId": 0, //(integer): 部门 ,
				"statusId": statusId, //(integer): 提交状态 ,
				"notes": notes, //(string): 备注 ,
				"id": id //(integer, optional): Id
			};
			dataModel.whOutputDetail = [];

			var productionList = $(".production-list");
			for(var i = 0; i < productionList.length; i++) {

				var whId = Number($(".production-list").eq(i).find(".warehouse").data("id"));
				var goodsId = Number($(".production-list").eq(i).find(".clothes-code").data("id"));
				var price = Number($(".production-list").eq(i).find(".price").text());
				var goodsUnit = $(".production-list").eq(i).find(".clothes-code").data("unit");
				var goodsNum = Number($(".production-list").eq(i).find(".goodsNum").text());
				var whOutputDetail = {
					"whId": whId, //(integer): 仓库 ,
					"goodsId": goodsId, //(integer): 货品 ,
					"storageId": 0, //(integer): 储位 ,
					"unit": goodsUnit, //(string): 主单位 ,
					"price": price, //(number): 单价 ,
					"unitCode": goodsUnit, //(string): 副单位 ,
					"unitCodeNumber": goodsNum, //(number): 副单位数量 ,
					"unitCodeConvertRate": 1, //(number): 副单位转换率 ,
					"notes": "111", //(string, optional): 备注 ,
					"sourceType": 0, //(integer, optional): 来源单类型 ,
					"sourceSerial": "", //(string, optional): 来源单编号 ,
					"sourceId": 0, //(integer, optional): 来源单id ,
					"sourceItemId": 0, //(integer, optional): 来源单明细id ,
					"batchDetail": [],
					"id": 0
				};
				dataModel.whOutputDetail.push(whOutputDetail);
			}
			var outSourceType = sessionStorage.getItem("outSourceType");
			if(outSourceType == abp.sourceType.WH_CHECK) {
				var orderInDetail = JSON.parse(sessionStorage.getItem("orderInDetail"));
				for(var i = 0; i < orderInDetail.whOutputDetail.length; i++) {
					dataModel.whOutputDetail[i].sourceType = orderInDetail.whOutputDetail[i].sourceType;
					dataModel.whOutputDetail[i].sourceSerial = orderInDetail.whOutputDetail[i].sourceSerial;
					dataModel.whOutputDetail[i].sourceId = orderInDetail.whOutputDetail[i].sourceId;
					dataModel.whOutputDetail[i].sourceItemId = orderInDetail.whOutputDetail[i].sourceItemId;
				}
			}

			//	        console.log(dataModel.whOutputDetail);

			abp.services.app.whOutputList.update(dataModel, option)
				.done(function(data) {
					//console.log(data);
					if(dataModel.statusId == abp.statusId.Draft) {

						muiTips.alertTips(' ', "修改成功", 1, 3, mui.back);
					} else {

						muiTips.alertTips(' ', "提交成功", 1, 3, mui.back);
					}
					sessionStorage.removeItem('createHome');
					getFunList.clearSessionStroage();

				})
				.fail(function(data) {

					muiTips.alertTips(' ', data.message, 2, 3, "default");
					globalInit.buttonReset(".btnDisabledText");
				});
		}

		mui("body").on("tap", ".mui-btn-sub", function() {
			globalInit.buttonInit(".mui-btn-sub");
			if(sessionStorage.changeOrderflag) {
				changeOutstock(abp.statusId.Finish);
			} else {
				newOutstock(abp.statusId.Finish);
			}

		});
		mui("body").on("tap", ".keep-draft", function() {
			globalInit.buttonInit(".keep-draft");
			if(sessionStorage.changeOrderflag) {
				changeOutstock(abp.statusId.Draft);
			} else {
				newOutstock(abp.statusId.Draft);
			}
		});

		mui("body").on("tap", ".setParm", function() {
			sessionStorage.removeItem("setParm");
			sessionStorage.setParm = getFunList.parmsNO;
			sessionStorage.money = "show";
			sessionStorage.productSelectSource = "productSelectSource";
		});
		mui("body").on("tap", ".mui-action-back", function() {
			//返回 清除本地数据
			sessionStorage.removeItem('createHome');
			getFunList.clearSessionStroage();
		});
		mui("body").on("tap", ".production-list", function() {
			var goodsSerial = $(this).find(".clothes-code").text();
			var goodsName = $(this).find(".name").text();
			var goodsId = $(this).find(".name").attr("data-id");
			var goodsUnit = $(this).find(".name").attr("data-unit");
			var goodsImg = $(this).find(".name").attr("data-img");
			var whName = $(this).find(".warehouse").text();
			var whId = $(this).find(".warehouse").attr("data-id");
			var goodsNumber = $(this).find(".goodsNum").text();
			var price = $(this).find(".price").text();
			var goodsInfo = {
				"goodsSerial": goodsSerial,
				"goodsName": goodsName,
				"goodsId": goodsId,
				"goodsUnit": goodsUnit,
				"goodsImg": goodsImg,
				"whName": whName,
				"whId": whId,
				"number": goodsNumber,
				"price": price
			};
			sessionStorage.goodsInfo = JSON.stringify(goodsInfo);
			//			document.location.href="../newStockDetail/productDetail.html";
		});
	});

	//判断是否启用审批
	var eHistory = require("eHistory");
	var isExamina = eHistory.funIsAuditSetting(abp.sourceType.WH_OUTPUT, examinaSourceId);
	if(isExamina === true) {
		$("#btnSubmitExamina").text("提交审核");
	} else {
		$("#btnSubmitExamina").text("提交");
	}

});